package cn.slipi.admin.controller;

import cn.slipi.admin.biz.ProducerBiz;
import cn.slipi.admin.biz.vo.producer.DropVo;
import cn.slipi.admin.biz.vo.producer.ProducerVo;
import cn.slipi.admin.biz.vo.producer.SyncProducerVo;
import cn.slipi.admin.common.annotation.Permission;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.response.JsonResponse;
import cn.slipi.admin.constants.AdminConstants;
import cn.slipi.admin.constants.permission.PermissionConstants;
import cn.slipi.admin.controller.form.producer.ListForm;
import cn.slipi.admin.controller.form.producer.SaveForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 生产者
 */
@Validated
@Controller
@ResponseBody
@RequestMapping(AdminConstants.CAR_API_PREFIX + "/producer")
public class ProducerController {


    @Autowired
    private ProducerBiz producerBiz;

    @PostMapping("/list")
    @Permission(value = {PermissionConstants.PRODUCER_LIST})
    public JsonResponse<PageDTO<ProducerVo>> getList(@Valid @RequestBody ListForm listForm) {
        JsonResponse<PageDTO<ProducerVo>> responseVo = new JsonResponse<>();
        PageDTO<ProducerVo> result = this.producerBiz.getList(listForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/drop/list")
    @Permission(value = {PermissionConstants.PRODUCER_DROP_LIST})
    public JsonResponse<List<DropVo>> getDropList() {
        JsonResponse<List<DropVo>> responseVo = new JsonResponse<>();
        List<DropVo> result = this.producerBiz.getDropList();
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/add")
    @Permission(value = {PermissionConstants.PRODUCER_ADD})
    public JsonResponse<ProducerVo> add(@Valid @RequestBody SaveForm saveForm) {
        JsonResponse<ProducerVo> responseVo = new JsonResponse<>();
        ProducerVo result = this.producerBiz.add(saveForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PutMapping("/update/{id}")
    @Permission(value = {PermissionConstants.PRODUCER_UPDATE})
    public JsonResponse<ProducerVo> update(@PathVariable("id") Integer id, @Valid @RequestBody SaveForm saveForm) {
        JsonResponse<ProducerVo> responseVo = new JsonResponse<>();
        ProducerVo result = this.producerBiz.update(id, saveForm);
        responseVo.setData(result);
        return responseVo;
    }

    @GetMapping("/detail/{id}")
    @Permission(value = {PermissionConstants.PRODUCER_DETAIL})
    public JsonResponse<ProducerVo> detail(@PathVariable("id") Integer id) {
        JsonResponse<ProducerVo> responseVo = new JsonResponse<>();
        ProducerVo result = this.producerBiz.detail(id);
        responseVo.setData(result);
        return responseVo;
    }


}
